package goodbaby.dklt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.DoubleClickExitHelper;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;

import goodbaby.dklt.R;
import goodbaby.dklt.fragment.HealthFragment;
import goodbaby.dklt.fragment.HomeFragment;
import goodbaby.dklt.fragment.SafeFragment;
import goodbaby.dklt.fragment.StudyFragment;

/**
 * Created by Jayden on 2016/4/19 16:36.
 *
 * @version V1.0
 * @Title: MainActivity
 * @Description: 主页面
 */

public class MainActivity extends BaseActivity implements View.OnClickListener, View.OnLayoutChangeListener {

    public static boolean isShow = false;
    public static FloatingActionButton leftCenterButton;
    public static LinearLayout llTabMain;
    private DoubleClickExitHelper doubleClickExit;
    private HealthFragment healthFragment;
    private HomeFragment homeFragment;
    private int keyHeight = 0;
    private LinearLayout llTabClass;
    private LinearLayout llTabHealth;
    private LinearLayout llTabHome;
    private LinearLayout llTabSafe;
    private LinearLayout rootLinearLayout;
    private SafeFragment safeFragment;
    private int screenHeight = 0;
    private StudyFragment studyFragment;

    private void bottomCenterButton() {
        int redActionButtonSize = getResources().getDimensionPixelSize(
                R.dimen.red_action_button_size);
        int redActionButtonMargin = getResources().getDimensionPixelOffset(
                R.dimen.action_button_margin);
        int redActionButtonContentSize = getResources().getDimensionPixelSize(
                R.dimen.red_action_button_content_size);
        int redActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.red_action_button_content_margin);
        int redActionMenuRadius = getResources().getDimensionPixelSize(
                R.dimen.red_action_menu_radius);
        int blueSubActionButtonSize = getResources().getDimensionPixelSize(
                R.dimen.blue_sub_action_button_size);
        int blueSubActionButtonContentMargin = getResources()
                .getDimensionPixelSize(
                        R.dimen.blue_sub_action_button_content_margin);
        ImageView fabIconStar = new ImageView(this);
//        fabIconStar.setImageResource(R.mipmap.tab_more_add);

        // 设置菜单按钮Button的宽、高，边距
        FloatingActionButton.LayoutParams starParams = new FloatingActionButton.LayoutParams(
                redActionButtonSize, redActionButtonSize);
        starParams.setMargins(redActionButtonMargin, redActionButtonMargin,
                redActionButtonMargin, redActionButtonMargin);
        fabIconStar.setLayoutParams(starParams);

        // 设置菜单按钮Button里面图案的宽、高，边距
        FloatingActionButton.LayoutParams fabIconStarParams = new FloatingActionButton.LayoutParams(
                redActionButtonContentSize, redActionButtonContentSize);
        fabIconStarParams.setMargins(redActionButtonContentMargin,
                redActionButtonContentMargin, redActionButtonContentMargin,
                redActionButtonContentMargin);

        final FloatingActionButton leftCenterButton = new FloatingActionButton.Builder(
                this).setContentView(fabIconStar, fabIconStarParams)
                .setBackgroundDrawable(R.mipmap.tab_more_add)
                .setPosition(FloatingActionButton.POSITION_BOTTOM_CENTER)
                .setLayoutParams(starParams).build();

        leftCenterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        SubActionButton.Builder lCSubBuilder = new SubActionButton.Builder(this);
        lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.button_action_blue_selector));

        //设置菜单中图标的参数
        FrameLayout.LayoutParams blueContentParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        blueContentParams.setMargins(blueSubActionButtonContentMargin,
                blueSubActionButtonContentMargin,
                blueSubActionButtonContentMargin,
                blueSubActionButtonContentMargin);

        lCSubBuilder.setLayoutParams(blueContentParams);

        //设置布局参数
        FrameLayout.LayoutParams blueParams = new FrameLayout.LayoutParams(blueSubActionButtonSize,
                blueSubActionButtonSize);
        lCSubBuilder.setLayoutParams(blueParams);

        ImageView lcIcon1 = new ImageView(this);
        ImageView lcIcon2 = new ImageView(this);
        ImageView lcIcon3 = new ImageView(this);
        ImageView lcIcon4 = new ImageView(this);

        lcIcon1.setImageResource(R.mipmap.add_menu_leave);
        lcIcon2.setImageResource(R.mipmap.add_menu_vedio);
        lcIcon3.setImageResource(R.mipmap.add_menu_photo);
        lcIcon4.setImageResource(R.mipmap.add_menu_msg);


        //setStartAngle(70).setEndAngle(-70)设置扩展菜单的位置
        final FloatingActionMenu leftCenterMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(lCSubBuilder.setContentView(lcIcon1, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon2, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon3, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon4, blueContentParams).build())
                .setRadius(redActionMenuRadius).setStartAngle(-40).setEndAngle(-140)
                .attachTo(leftCenterButton).build();
        lcIcon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent localIntent = new Intent(MainActivity.this, VedioRecordActivity.class);
                startActivity(localIntent);
            }
        });
        lcIcon3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SendParentsCircleActivity.class);
                intent.putExtra("msgType", 1);
                startActivity(intent);
            }
        });
        lcIcon4.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SendParentsCircleActivity.class);
                intent.putExtra("msgType", 1);
                startActivity(intent);
            }
        });
        lcIcon1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AttendanceActivity.class);
                startActivity(intent);
            }
        });
    }


    private void setListeners() {
        this.llTabHome.setOnClickListener(this);
        this.llTabClass.setOnClickListener(this);
        this.llTabHealth.setOnClickListener(this);
        this.llTabSafe.setOnClickListener(this);
    }

    public void closeTab() {
        llTabMain.setVisibility(View.GONE);
    }

    public int getLayoutResId() {
        return R.layout.activity_main;
    }

    public void initData() {
        super.initData();
        this.doubleClickExit = new DoubleClickExitHelper(this);
        this.doubleClickExit.setTimeInterval(3000);
        this.doubleClickExit.setToastContent("再按一次退出应用!");
        this.screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        this.keyHeight = (this.screenHeight / 3);
        setTabSelection(0);
        bottomCenterButton();
    }

    public void initView() {
        super.initView();
        rootLinearLayout = (LinearLayout) findViewById(R.id.ll_root);
        llTabMain = (LinearLayout) findViewById(R.id.ll_main_tab);
        llTabHome = (LinearLayout) findViewById(R.id.ll_tab_home);
        llTabClass = (LinearLayout) findViewById(R.id.ll_tab_class);
        llTabHealth = (LinearLayout) findViewById(R.id.ll_tab_health);
        llTabSafe = (LinearLayout) findViewById(R.id.ll_tab_safe);
        llTabHome.setSelected(true);
        llTabClass.setSelected(false);
        llTabHealth.setSelected(false);
        llTabSafe.setSelected(false);
        this.rootLinearLayout.addOnLayoutChangeListener(this);
        setListeners();
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     * @param index 每个tab页对应的下标。0主页,1用户,2更多
     */
    public void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    //transaction.add(R.id.fragment_container, mHomeFragment);
                    transaction.add(R.id.fragment_container, homeFragment);
                } else {
                    homeFragment.setUserVisibleHint(true);
                    transaction.show(homeFragment);
                }
                break;

            case 1:

                if (studyFragment == null) {
                    studyFragment = new StudyFragment();
                    //transaction.add(R.id.fragment_container, mHomeFragment);
                    transaction.add(R.id.fragment_container, studyFragment);
                } else {
                    studyFragment.setUserVisibleHint(true);
                    transaction.show(studyFragment);
                }

                break;
            case 2:
                if (healthFragment == null) {
                    healthFragment = new HealthFragment();
                    //transaction.add(R.id.fragment_container, mHomeFragment);
                    transaction.add(R.id.fragment_container, healthFragment);
                } else {
                    healthFragment.setUserVisibleHint(true);
                    transaction.show(healthFragment);
                }

                break;
            case 3:
                if (safeFragment == null) {
                    safeFragment = new SafeFragment();
                    //transaction.add(R.id.fragment_container, mHomeFragment);
                    transaction.add(R.id.fragment_container, safeFragment);
                } else {
                    safeFragment.setUserVisibleHint(true);
                    transaction.show(safeFragment);
                }

                break;

            default:
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (healthFragment != null) {
            transaction.hide(healthFragment);
        }
        if (safeFragment != null) {
            transaction.hide(safeFragment);
        }
        if (studyFragment != null) {
            transaction.hide(studyFragment);
        }
    }

    //防止重叠
    @Override
    public void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

//    private ArrayList<MyOnTouchListener> onTouchListeners = new ArrayList<MyOnTouchListener>(
//            10);
//
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        for (MyOnTouchListener listener : onTouchListeners) {
//            listener.onTouch(ev);
//        }
//        return super.dispatchTouchEvent(ev);
//    }
//
//    public void registerMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
//        onTouchListeners.add(myOnTouchListener);
//    }
//
//    public void unregisterMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
//        onTouchListeners.remove(myOnTouchListener);
//    }
//
//    public interface MyOnTouchListener {
//        public boolean onTouch(MotionEvent ev);
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_tab_home:
                setTabSelection(0);
                llTabHome.setSelected(true);
                llTabClass.setSelected(false);
                llTabHealth.setSelected(false);
                llTabSafe.setSelected(false);
                break;
            case R.id.ll_tab_class:
                setTabSelection(1);
                llTabHome.setSelected(false);
                llTabClass.setSelected(true);
                llTabHealth.setSelected(false);
                llTabSafe.setSelected(false);
                break;
            case R.id.ll_tab_health:
                setTabSelection(2);
                llTabHome.setSelected(false);
                llTabClass.setSelected(false);
                llTabHealth.setSelected(true);
                llTabSafe.setSelected(false);
                break;
            case R.id.ll_tab_safe:
                setTabSelection(3);
                llTabHome.setSelected(false);
                llTabClass.setSelected(false);
                llTabHealth.setSelected(false);
                llTabSafe.setSelected(true);
                break;
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
    }


    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        return this.doubleClickExit.onKeyDown(paramInt, paramKeyEvent);
    }

    public void onLayoutChange(View paramView, int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6, int paramInt7, int paramInt8) {
        if ((paramInt8 != 0) && (paramInt4 != 0) && (paramInt8 - paramInt4 > this.keyHeight))
            this.homeFragment.closeBottom();
        while ((paramInt8 == 0) || (paramInt4 == 0) || (paramInt4 - paramInt8 <= this.keyHeight))
            return;
        this.homeFragment.showBottom();
    }

    public void openTab() {
        llTabMain.setVisibility(View.VISIBLE);
    }

}
